import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaLeftRightLayout,WeaLocaleProvider,WeaTools,WeaNewScroll,WeaOrgTree} from 'ecCom';
import {Menu,Tooltip} from 'antd';
import TaskMain from './TaskMain';
import TaskReport from '../task/taskreport/TaskReport';
import TaskExport from '../task/taskexecl/ExportIndex';
import * as Util from '../../util/Util';
const Item = Menu.Item;
const getLabel = WeaLocaleProvider.getLabel;
@observer
export default class TaskIndex extends React.Component {
    constructor(props) {
        super(props);
        this.state ={
            cunrrentkey:"",//当前菜单
            toptitle:"",//当前菜单名称
            sorttype:"1",
            showtype: "1",
            status:"1",
            viewId:"0",
            newMap:{},
            isRight:"",
            _height:Util.getCurrentHeight(),
            subcount:0,
            _time:new Date(),
            _selectKey:[]
        }
        this.menumap = {"1":getLabel(19865,"我的任务"),"3":getLabel(382482,"我负责的任务"),
                        "4":getLabel(382483,"我参与的任务"),"6":getLabel(382484,"我关注的任务"),
                        "2":getLabel(382485,"我创建的任务"),"8":getLabel(382486,"我分配的任务"),
                        "5":getLabel(382492,"分享给我的任务"),"0":getLabel(382488,"所有任务"),"7":getLabel(382494,"已完成新反馈任务")
                       }
    }
    componentWillMount() {//一些初始化请求
        WeaTools.callApi('/api/workrelate/common/checkHasRigth', 'GET', {id:2}).then((data) => {
            if(data.status && data.isRight){
                this.setState({isRight:"1",subcount:data.subcount});
                this.seachSaveView();
            }else{
                this.setState({isRight:"0"});
            }
        })
        
    }
    seachSaveView(){
        let p_condtype = Util.getParams("condtype");
        let w_condtype = sessionStorage.getItem("taskCondtype");
        let w_status = sessionStorage.getItem("taskStatus");
        WeaTools.callApi('/api/workrelate/task/taskViewInfo', 'POST', {operation:"search"}).then((data) => {
            if (data.status) {
                let condtype = data.condtype || "1";
                let _status = data.cstatus || "1";
                let showtype = data.showtype || "1"
                if(p_condtype!=""){
                    condtype = p_condtype;
                }
                if(w_condtype && w_condtype!=""){
                    if(w_condtype=="9"){
                        w_condtype = "0";
                    }
                    condtype = w_condtype;
                }
                if(w_status && w_status!=""){
                    _status = w_status;
                }
                if(data.viewId==0){
                    this.setState({cunrrentkey:condtype,toptitle:this.menumap[condtype],viewId:"0",status:_status});
                }else{
                    this.setState({cunrrentkey:condtype,toptitle:this.menumap[condtype],sorttype:data.sorttype,showtype,
                        status:_status,viewId:data.viewId});
                }
                
            }
        });
        this.onCheckNew();
    }
    onCheckNew=()=>{
        WeaTools.callApi('/api/workrelate/task/checknew', 'GET', {}).then((data) => {
            if (data.status) {
                this.setState({newMap:data.newMap});
            }
        });
    }
    handleClick = (e) => {
        this.setState({
            cunrrentkey: e.key,
            toptitle:this.menumap[e.key],
            _time:new Date(),
            _selectKey:[]
        });
    }
    chooseUser(e){
      let hrmId = '', hrmName = undefined;
      if (e.selected) {
          hrmId = e.node.props.id;
          hrmName = e.node.props.name;
      }
      if(hrmId) {
        let hrmid = [{id:hrmId,name:hrmName}];
        this.refs.taskMainRef.wrappedInstance.advanceSearch(hrmid);
      }
    }
    onNodeSelect(ckey){
        this.setState ({_selectKey:ckey});
      }
    render() {
        const{cunrrentkey,toptitle,sorttype,showtype,status,viewId,newMap,isRight,_height,subcount,_time,_selectKey} = this.state;
        if(isRight==""){
            return <div></div>;
        }
        if(isRight=="0"){
            return Util.renderAlertPage('<span style="color:red">'+getLabel(509275,"您的系统未开启任务管理模块")+'</span>','','','',this.props.ecId || '');
        }
        let menu = <Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@kuqi0o`} onClick={this.handleClick}
        style={{ width: 248 }}
        defaultOpenKeys={[cunrrentkey]}
        selectedKeys={[cunrrentkey]}
        mode="inline">
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@y7nt3i`} key="1" title={getLabel(382481,"包括我负责的任务、我参与的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(19865,"我的任务")}</span>
               {newMap && newMap["1"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@zsols3`} title={getLabel(508798,newMap["1"]+"条未读或有新反馈的任务").replace("$workcode$",newMap["1"])}><span className="workrelate-unread">{newMap["1"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@h6vo59`} key="3" title={getLabel(382482,"我负责的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382482,"我负责的任务")}</span>
               {newMap && newMap["3"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@8elydp`} title={getLabel(508798,newMap["3"]+"条未读或有新反馈的任务").replace("$workcode$",newMap["3"])}><span className="workrelate-unread">{newMap["3"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@13ju99`} key="4" title={getLabel(382483,"我参与的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382483,"我参与的任务")}</span>
               {newMap && newMap["4"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@1tyxzc`} title={getLabel(508798,newMap["4"]+"条未读或有新反馈的任务").replace("$workcode$",newMap["4"])}><span className="workrelate-unread">{newMap["4"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@psxswf`} key="6" title={getLabel(382484,"我关注的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382484,"我关注的任务")}</span>
               {newMap && newMap["6"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@3iucbj`} title={getLabel(508798,newMap["6"]+"条未读或有新反馈的任务").replace("$workcode$",newMap["6"])}><span className="workrelate-unread">{newMap["6"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@jo4r44`} key="2" title={getLabel(382485,"我创建的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382485,"我创建的任务")}</span>
               {newMap && newMap["2"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@8rg4ps`} title={getLabel(508802,newMap["2"]+"条有新反馈的任务").replace("$workcode$",newMap["2"])}><span className="workrelate-unread">{newMap["2"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@82qd9o`} key="8" title={getLabel(382486,"我分配的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382486,"我分配的任务")}</span>
               {newMap && newMap["8"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@9pctx5`} title={getLabel(508802,newMap["8"]+"条有新反馈的任务").replace("$workcode$",newMap["8"])}><span className="workrelate-unread">{newMap["8"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@7u1ibx`} key="5" title={getLabel(382492,"分享给我的任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382492,"分享给我的任务")}</span>
               {newMap && newMap["5"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@zm6wf8`} title={getLabel(508798,newMap["5"]+"条未读或有新反馈的任务").replace("$workcode$",newMap["5"])}><span className="workrelate-unread">{newMap["5"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@2m6it5`} key="0" title={getLabel(382488,"所有任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382488,"所有任务")}</span></Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@uc6khl`} key="7" title={getLabel(382494,"已完成新反馈任务")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382494,"已完成新反馈任务")}</span>
               {newMap && newMap["7"]>0 && <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@b0s8hu`} title={getLabel(508803,newMap["7"]+"条已完成有新反馈的任务").replace("$workcode$",newMap["7"])}><span className="workrelate-unread">{newMap["7"]}</span></Tooltip >}
            </Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@61lj07`} key="9" title={getLabel(382489,"任务执行分析")} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(382489,"任务执行分析")}</span></Item>
            <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@5rzyfb`} key="10" title={getLabel(520758,'任务导出')} style={{height:'30px',lineHeight:"30px"}}><span>{getLabel(520758,'任务导出')}</span></Item>
        </Menu>;
        return (
            <div className="wea-workrelate-task-menu wea-workrelate-common-menu">
                <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@2vr33o`}
                    leftCom={<div className="common-menu-title">{menu}
                    {cunrrentkey && cunrrentkey==0 && subcount>0 && <div><div className="common-menu-subtitle">{getLabel(442,"下属")}</div>
                        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@jpykw8`} height={_height-395}>
                            <WeaOrgTree ecId={`${this && this.props && this.props.ecId || ''}_WeaOrgTree@eocwpe`} dataUrl="/api/hrm/base/getHrmResourceTree" rootKey="rootManager"
                                treeNodeClick={(e)=>this.chooseUser(e)} selectedKeys={_selectKey} onNodeSelect={(ckey)=>this.onNodeSelect(ckey)}/>
                        </WeaNewScroll></div>}
                    </div>}
                    leftWidth={248}
                    onCollapse={showLeft =>{}}>
                    <div>{cunrrentkey &&(cunrrentkey==9?<TaskReport ecId={`${this && this.props && this.props.ecId || ''}_TaskReport@vbdgx2`}/>
                    :cunrrentkey==10?<TaskExport ecId={`${this && this.props && this.props.ecId || ''}_TaskExport@9h16kz`}/>
                    :<TaskMain ecId={`${this && this.props && this.props.ecId || ''}_TaskMain@w0bnoj`} condtype={cunrrentkey} ref="taskMainRef" menuid={cunrrentkey} _time={_time} toptitle={toptitle} sorttype={sorttype} showtype={showtype} status={status} viewId={viewId} onCheckNew={this.onCheckNew}/>)}</div>
                </WeaLeftRightLayout>
            </div>
        )
    }
};